/*
 * @Author: huangtianyang 916072572@qq.com
 * @Date: 2023-04-19 10:10:28
 * @LastEditors: huangtianyang
 * @LastEditTime: 2023-10-09 11:12:37
 * @FilePath: \cxl-h5\src\pages\personalCenter\invoice\index\index.ts
 */
import { noInvoiceOrderList } from '@/apis/invoice'
import { CxlMessage } from '@/components/message'
import { storage } from '@/store/sessionStorage'
import { BasicData } from '@/utils/basicData'
import { reactive, ref } from 'vue'

export default class Invoice extends BasicData {
    //大于100才能开票
    minMoney = ref(0.01)
    //待开票列表
    list: any[] = reactive([])
    //选择的订单list
    selectList: any[] = reactive([])
    //是否全部勾选
    isSelectAll = ref(false)
    //下拉刷新
    refreshLoading = ref(false)
    //内容数据刷新
    dataLoading = ref(false)
    //内容结束刷新
    dataFinished = ref(false)
    //内容默认显示
    contentVisible = ref(true)
    //选择的订单价格总和
    selectOrderPrice = ref('0.00')
    //选择订单总数
    selectOrderTotal = ref(0)
    //分页
    pagination: any = reactive({
        pageNo: 1,
        pageSize: 10
    })
    confirmPopup = reactive({
        visible: false,
        content: '开票金额累计需要大于 100 元方可开票！',
        title: '温馨提示'
    })

    constructor() {
        super()
    }

    //打开开票记录
    openHistory = () => {
        this.router.push('/invoice/history')
    }

    //点击确定选择的订单列表
    onConfirm = () => {
        if (this.selectList.length === 0) {
            CxlMessage.error('请选择需要开票的订单！')
            return false
        }
        if (Number(this.selectOrderPrice.value) < this.minMoney.value) {
            this.confirmPopup.visible = true
            return false
        }
        storage.set('invoiceOrderIds', JSON.stringify(this.selectList))
        this.router.push({ path: '/invoice/make-out', query: { amount: this.selectOrderPrice.value } })
    }
    //拉取数据
    getData = () => {
        noInvoiceOrderList(this.pagination.pageNo, this.pagination.pageSize).then(res => {
            if (this.refreshLoading.value) {
                this.refreshLoading.value = false
            }
            // 加载状态结束
            this.dataLoading.value = false
            this.pagination.pageNo += 1
            if (res.data.value === 200) {
                res.data.data.records.forEach((item: any) => {
                    item.select = false
                    this.list.push(item)
                })

                this.contentVisible.value = this.list.length ? true : false
                if (res.data.data.records.length < this.pagination.pageSize) {
                    this.dataFinished.value = true
                    console.log('over')
                }
            }
        })
    }
    //刷新方法
    onRefresh = () => {
        console.log('下拉刷新')
        this.refreshLoading.value = true
        this.dataFinished.value = false
        this.dataLoading.value = true
        this.pagination.pageNo = 1
        this.list.length = 0
        this.getData()
    }
    //加载数据
    onLoad = () => {
        console.log('加载数据')
        this.getData()
    }
    //返回首页
    onOpenDetection = () => {
        this.router.go(-1)
    }
    //点击全选
    selectALL = () => {
        if (this.list.length) {
            let p = 0
            let n = 0
            this.selectList.length = 0
            this.isSelectAll.value = !this.isSelectAll.value
            if (this.isSelectAll.value) {
                this.list.forEach((item: any) => {
                    item.select = true
                    p += Number(item.amount)
                    n++
                    this.selectList.push(item.orderId)
                })
            } else {
                this.list.forEach((item: any) => {
                    item.select = false
                })
                p = 0.0
                n = 0
            }
            this.selectOrderPrice.value = p.toFixed(2).toString()
            this.selectOrderTotal.value = n
        }
    }
    //选择订单
    onSelectOrder = (index: number) => {
        this.list[index].select = !this.list[index].select
        if (this.isSelectAll.value) {
            this.isSelectAll.value = false
        }

        let p = 0
        let n = 0
        this.selectList.length = 0
        this.list.forEach((item: any) => {
            if (item.select == true) {
                p += Number(item.amount)
                n++
                this.selectList.push(item.orderId)
            }
        })
        this.selectOrderPrice.value = p.toFixed(2).toString()
        this.selectOrderTotal.value = n
    }
}
